<script setup>

// 导入静态资源
import { ref } from 'vue';
import logoimg from '@/assets/gym.jpeg';
import useAdminStore from '@/stores/admin';
import { useRouter } from 'vue-router';
import { useTokenStore } from '@/stores/token';
import request from '@/utils/request';
import { adminInfoServe } from '@/api/admin';
import { ElMessage } from 'element-plus';

// 定义响应式数据
const isSidebarClosed = ref(false);
const isDarkMode = ref(false);
const modeText = ref('夜间模式');
const menuStatu = ref("学员")
const route = useRouter()
const useToken = useTokenStore()
const studentsNum = ref()
const coachNum = ref()
const adminData = ref({})


// 切换侧边栏的方法
const toggleSidebar = () => {
  isSidebarClosed.value = !isSidebarClosed.value;

};

// 打开侧边栏的方法（例如点击搜索框时）
const openSidebar = () => {
  isSidebarClosed.value = false;
};

// 切换模式的方法
const toggleMode = () => {
  isDarkMode.value = !isDarkMode.value;
  if (isDarkMode.value) {
    modeText.value = '日间模式';
  } else {
    modeText.value = '夜间模式';
  }
};

// 切换菜单的方法
const changeMenu = (menuValue) => {
  menuStatu.value = menuValue;
};

// 读取登录信息
const useAdmin = useAdminStore()
const getAdminInfo = async () => {
  let admin = await adminInfoServe()
  useAdmin.setAdmin(admin.data.data)
}


// 获取统计数据
const getNums = () => {
  request.get("showData").then((res) => {
    studentsNum.value = res.data.data.student_num;
    coachNum.value = res.data.data.coach_num;
  });

}

// 退出登录
const exitLogin = () => {
  useAdmin.removeAdmin();
  useToken.removeToken();
  route.push("login")
  ElMessage.success("登出成功")
}
getNums()
getAdminInfo()
</script>


<template>
  <div class="main" :class="{ dark: isDarkMode }">
    <!-- 左边侧边栏 -->
    <nav class="sidebar" :class="{ close: isSidebarClosed }">
      <header>
        <div class="image-text">
          <span class="image">
            <img :src="logoimg" alt="">
          </span>
          <div class="text logo-text">
            <span class="name">律动健身房</span>
            <span class="profession">Build yourself</span>
          </div>
        </div>
        <i class='bx bx-chevron-right toggle' @click="toggleSidebar"></i>
      </header>
      <div class="menu-bar">
        <div class="menu">
          <li class="search-box">
            <i class='bx bx-search icon' @click="openSidebar"></i>
            <input type="text" placeholder="搜索一下...">
          </li>
          <ul class="menu-links">
            <li :class="{ ischoosed: menuStatu == '学员' }" @click="changeMenu('学员')">
              <router-link to="student">
                <a href="#">
                  <i class='bx bx-user icon' :style="{ 'color': menuStatu == '学员' ? '#fff' : 'var(--text-color)' }"></i>
                  <span class="text nav-text"
                    :style="{ 'color': menuStatu == '学员' ? '#fff' : 'var(--text-color)' }">学员</span>
                </a>
              </router-link>
            </li>
            <li :class="{ ischoosed: menuStatu == '教练' }" @click="changeMenu('教练')">
              <router-link to="coach">
                <a href="#">
                  <i class='bx bxs-graduation icon'
                    :style="{ 'color': menuStatu == '教练' ? '#fff' : 'var(--text-color)' }"></i>
                  <span class="text nav-text"
                    :style="{ 'color': menuStatu == '教练' ? '#fff' : 'var(--text-color)' }">教练</span>
                </a>
              </router-link>
            </li>
            <li :class="{ ischoosed: menuStatu == '我的' }" @click="changeMenu('我的')">
              <router-link to="admin">
                <i class='bx bx-home icon' :style="{ 'color': menuStatu == '我的' ? '#fff' : 'var(--text-color)' }"></i>
                <span class="text nav-text"
                  :style="{ 'color': menuStatu == '我的' ? '#fff' : 'var(--text-color)' }">我的</span>
              </router-link>
            </li>
          </ul>
        </div>
        <div class="bottom-content">
          <li class="" @click="exitLogin">
            <a href="#">
              <i class='bx bx-log-out icon'></i>
              <span class="text nav-text">登出</span>
            </a>
          </li>
          <li class="mode">
            <div class="sun-moon">
              <i class='bx bx-moon icon moon'></i>
              <i class='bx bx-sun icon sun'></i>
            </div>
            <span class="mode-text text">{{ modeText }}</span>
            <div class="toggle-switch" @click="toggleMode">
              <span class="switch"></span>
            </div>
          </li>
        </div>
      </div>
    </nav>
    <!-- 右侧主界面 -->
    <section class="home" :style="{ 'width': isSidebarClosed ? 'calc(100% - 90px)' : 'calc(100% - 230px)' }">
      <el-container>
        <!-- 头部区域 -->
        <el-header>
          <div class="top-left">
            <span>监控 / <span style="color:var(--header-menu-text-color)">{{ menuStatu }}</span></span>
          </div>
          <div class="top-right">
            <router-link to="student">
              <a href="" title="首页"><i @click="changeMenu('学员')" class='bx bx-smile'></i></a>
            </router-link>
            <router-link to="admin">
              <a href="" title="消息"><i class='bx bx-bell' @click="changeMenu('我的')"></i></a>
            </router-link>
            <router-link to="admin">
              <a href="#" title="我的">
                <i class='bx bx-user-circle' @click="changeMenu('我的')"></i>
              </a>
            </router-link>
            <span>{{ useAdmin.admin.name }}</span>
          </div>
        </el-header>
        <!-- 中间区域 -->
        <el-main>
          <!-- 中上方卡片 -->
          <div class="content-top">
            <el-card style="height:100%" shadow="never">
              <div class="card-left">
                <el-avatar :size="70" src="https://ali-cdn.educoder.net/images/avatars/User/768127?t=1732017449" />
                <div class="avatar-text">
                  <p class="text-bold">Hi,{{ useAdmin.admin.name }}，今天又是充满活力的一天！</p>
                  <p style="color: var(--text-color);">今日多云转晴，12℃ - 25℃!</p>
                </div>
              </div>

              <div class="card-right">
                <span style="color: var(--text-color);">学员</span>
                <span style="color: var(--text-color);">教练</span>
                <span style="color: var(--text-color);">消息</span>
                <span class="text-bold">{{ studentsNum }}</span>
                <span class="text-bold">{{ coachNum }}</span>
                <span class="text-bold">0</span>
              </div>
              <div class="clearfix"></div>
            </el-card>
          </div>
          <!-- 中间内容 -->
          <div class="content-center">
            <router-view>

            </router-view>
          </div>
        </el-main>
        <el-footer>律动健身 ©2024 Created by 户用质素高</el-footer>

      </el-container>
    </section>
  </div>
</template>



<style scoped>
/* 引入谷歌字体,图标 */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
@import url('https://unpkg.com/boxicons@2.1.1/css/boxicons.min.css');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}


.main {
  min-height: 100vh;
  background-color: var(--body-color);
  transition: var(--tran-05);
}

::selection {
  background-color: var(--primary-color);
  color: #fff;
}

.main.dark {
  --body-color: #18191a;
  --sidebar-color: #242526;
  --primary-color: #3a3b3c;
  --primary-color-light: #3a3b3c;
  --toggle-color: #fff;
  --text-color: #ccc;
  --header-text-color: #ccc;
  --header-menu-text-color: #ccc;
}

/* ===== 侧栏 ===== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 230px;
  padding: 10px 14px;
  background: var(--sidebar-color);
  transition: var(--tran-05);
  z-index: 100;
}

.sidebar.close {
  width: 88px;
}

.sidebar li {
  height: 50px;
  list-style: none;
  display: flex;
  align-items: center;
  margin-top: 10px;
}

.el-card__body {
  overflow: hidden;
}

.sidebar header .image,
.sidebar .icon {
  min-width: 60px;
  border-radius: 6px;
}

.sidebar .icon {
  min-width: 60px;
  border-radius: 6px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.sidebar .text,
.sidebar .icon {
  color: var(--text-color);
  transition: var(--tran-03);
}

.sidebar .text {
  font-size: 17px;
  font-weight: 500;
  white-space: nowrap;
  opacity: 1;
}

.sidebar.close .text {
  opacity: 0;
}

.sidebar header {
  position: relative;
  margin-top: 5px;
}

.sidebar header .image-text {
  display: flex;
  align-items: center;
}

.sidebar header .logo-text {
  display: flex;
  flex-direction: column;
}

header .image-text .name {
  margin-top: 2px;
  font-size: 18px;
  font-weight: 600;
}

header .image-text .profession {
  font-size: 16px;
  margin-top: -2px;
  display: block;
}

.sidebar header .image {
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar header .image img {
  width: 40px;
  border-radius: 6px;
}

.sidebar header .toggle {
  position: absolute;
  top: 28%;
  right: -25px;
  transform: translateY(-50%) rotate(180deg);
  height: 25px;
  width: 25px;
  background-color: var(--primary-color);
  color: var(--sidebar-color);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  cursor: pointer;
  transition: var(--tran-05);
}

.main.dark .sidebar header .toggle {
  color: var(--text-color);
}

.sidebar.close .toggle {
  transform: translateY(-50%) rotate(0deg);
}

.sidebar .menu {
  margin-top: 40px;
}

.sidebar li.search-box {
  border-radius: 6px;
  background-color: var(--primary-color-light);
  cursor: pointer;
  transition: var(--tran-05);
}

.sidebar li.search-box input {
  height: 100%;
  width: 100%;
  outline: none;
  border: none;
  background-color: var(--primary-color-light);
  color: var(--text-color);
  border-radius: 6px;
  font-size: 17px;
  font-weight: 500;
  transition: var(--tran-05);
}

.sidebar li a {
  list-style: none;
  height: 100%;
  background-color: transparent;
  display: flex;
  align-items: center;
  height: 100%;
  width: 100%;
  border-radius: 6px;
  text-decoration: none;
  transition: var(--tran-03);
}

.ischoosed {
  background-color: var(--primary-color);
  list-style: none;
  display: flex;
  align-items: center;
  height: 100%;
  width: 100%;
  border-radius: 6px;
  text-decoration: none;
  transition: var(--tran-03);

}

.sidebar li a:hover {
  background-color: var(--primary-color);
}

.sidebar li a:hover .icon,
.sidebar li a:hover .text {
  color: var(--sidebar-color);
}

.main.dark .sidebar li a:hover .icon,
.main.dark .sidebar li a:hover .text {
  color: var(--text-color);
}

.sidebar .menu-bar {
  height: calc(100% - 55px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow-y: scroll;
}

.menu-bar::-webkit-scrollbar {
  display: none;
}

.sidebar .menu-bar .mode {
  border-radius: 6px;
  background-color: var(--primary-color-light);
  position: relative;
  transition: var(--tran-05);
}

.menu-bar .mode .sun-moon {
  height: 50px;
  width: 60px;
}

.mode .sun-moon i {
  position: absolute;
}

.mode .sun-moon i.sun {
  opacity: 0;
}

.main.dark .mode .sun-moon i.sun {
  opacity: 1;
}

.main.dark .mode .sun-moon i.moon {
  opacity: 0;
}

.menu-bar .bottom-content .toggle-switch {
  position: absolute;
  right: 0;
  height: 100%;
  min-width: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  cursor: pointer;
}

.toggle-switch .switch {
  position: relative;
  height: 22px;
  width: 40px;
  border-radius: 25px;
  background-color: var(--toggle-color);
  transition: var(--tran-05);
}

.switch::before {
  content: '';
  position: absolute;
  height: 15px;
  width: 15px;
  border-radius: 50%;
  top: 50%;
  left: 5px;
  transform: translateY(-50%);
  background-color: var(--sidebar-color);
  transition: var(--tran-04);
}

.main.dark .switch::before {
  left: 20px;
}

.home {
  position: absolute;
  top: 0;
  top: 0;
  left: 230px;
  height: 100vh;
  width: calc(100% - 250px);
  background-color: var(--body-color);
  transition: var(--tran-05);
}

.home .text {
  font-size: 30px;
  font-weight: 500;
  color: var(--text-color);
  padding: 12px 60px;
}

.sidebar.close~.home {
  left: 88px;
  height: 100vh;

}

.main.dark .home .text {
  color: var(--text-color);
}

.el-header {
  background-color: var(--sidebar-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  height: 55px;
  color: var(--header-text-color);
}

.el-main {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 23px 20px 35px 20px;
}

.el-container {
  height: 100%;
}

.el-header i {
  min-width: 55px;
  border-radius: 6px;
  height: 100%;
  font-size: 23px;
  color: var(--text-color);
}

.top-left {
  font-size: 17px;
  font-weight: 500;
  white-space: nowrap;
  opacity: 1;
  display: flex;
}

.top-right {
  font-size: 17px;
  font-weight: 500;
  white-space: nowrap;
  opacity: 1;
  display: flex;
  margin-top: 6px;

}

el-container {
  display: flex;
}

.content-top {
  height: 17%;
}

.content-center {
  height: 81%;

}

.card-left {
  float: left;
  color: var(--header-text-color);

}

.text-bold {
  font-weight: 800;
  font-size: 19px;
  color: #18191a !important;
  line-height: 42px;
}

.card-right {
  float: right;
  display: flex;
  width: 250px;
  height: 75.5px;
  flex-wrap: wrap;
  margin-right: -10px;
  justify-content: space-between;
  align-items: center;
}

.card-right span {
  width: 80px;
  text-align: center;
}


.avatar-text {
  float: right;
  margin-left: 15px;
}

.el-card {
  height: 100%;
  padding: 0 10px;
  border-radius: 10px;
}

.clearfix {
  clear: both;
}

.el-footer {
  position: fixed;
  font-size: 14px;
  left: 47%;
  top: 96.5%;
  color: #666;
}
</style>